package com.openstack4j.qx.rest;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.api.compute.ServerService;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.storage.block.Volume;
import org.openstack4j.model.storage.block.Volume.Status;
import org.openstack4j.model.storage.block.VolumeAttachment;
import org.openstack4j.openstack.OSFactory;
import org.openstack4j.openstack.compute.internal.ServerServiceImpl;

import com.openstack4j.qx.pojo.BSResp;
import com.openstack4j.qx.pojo.BaseResp;

@Path("/BlockStorageRest")
public class BlockStorageRest {
	/*
	 * 创建块从存储
	 */
	@POST
	@Path("/CreateBSReq")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp CreateBSReq(@Context HttpServletRequest request){
		
		String ParamFlag = request.getParameter("ParamFlag");
		String BSTemplateId = request.getParameter("BSTemplateId");
		
		//获取<Complex> BSParamArray的数据
//		String BSParamArray = request.getParameter("BSParamArray");
		String Tier = request.getParameter("Tier");
		String VolNum = request.getParameter("VolNum");
		String VolSize = request.getParameter("VolSize");
		
		String ResourceID = request.getParameter("ResourceID");
		String BSName = request.getParameter("BSName");
		String AppID = request.getParameter("AppID");
		String AppName = request.getParameter("AppName");
		
		//实例化返回值对象
		BSResp bSResp = new BSResp();
		
		
		try{
			
			//创建磁盘volume操作
			//登录
			OSClientV2 os = OSFactory.builderV2()
	                .endpoint("http://10.50.0.11:5000/v2.0")
		            .credentials("admin","ADMIN_PASS")
		            .tenantName("admin")
		            .authenticate();
			System.out.println("authentication complete");
			//转换string Tier 为 int tier
			Integer tier = Integer.parseInt(Tier);
			//定义一个变量
			String volume_type;
			//判断选择磁盘性能,并传给volumeType
			if(tier == 0){   
				volume_type="SSD";   //性能型
			}else{
				volume_type="SATA";  //容量型
			}
			
			//转换string VolNum 为 int volNum
			Integer volNum = Integer.parseInt(VolNum);
			//转换string VolSize 为 int volSize
			Integer volSize = Integer.parseInt(VolSize);
			//创建块存储volume	
			//按照bSParamArray.getVolNum，创建几个volume
			for(int i=0;i<volNum;i++){
				Volume volume = os.blockStorage().volumes()
		             .create(Builders.volume()
		                .name(volume_type + "性能型磁盘")
		                .volumeType(volume_type)   // 配置容量型和性能型存储
		                .description(volume_type + "性能型磁盘")
		                .size(volSize)
		                .build()
		             );
				bSResp.setBSID("成功创建volume"+volume.getId());
			}
			
			
			
			
			return bSResp;

		} catch (Exception e) {
			e.printStackTrace();
			// 异常信息
			bSResp.setFaultstring(e.getMessage());
		}
		return bSResp;
	
	}
	
	
	
	
	/**
	 * 删除块存储
	 */
	@GET
	@Path("/DeleteBSReq")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp DeleteBSReq(@Context HttpServletRequest request){
		
		String BSID = request.getParameter("BSID");
		
		BaseResp baseResp = new BaseResp();
		
		try{
			//登录
			OSClientV2 os = OSFactory.builderV2()
		            .endpoint("http://10.50.0.11:5000/v2.0")
		        .credentials("admin","ADMIN_PASS")
		        .tenantName("admin")
		        .authenticate();
			System.out.println("authentication complate");
			
			// delete specific volume by id
			ActionResponse actionResponse = os.blockStorage().volumes().delete(BSID);

			baseResp.setFaultstring(actionResponse.getFault());
				
			return baseResp;
		}catch(Exception e){
			
			e.printStackTrace();
			// 异常信息
			baseResp.setFaultstring(e.getMessage());
		}
		
		return baseResp;
	}
	
	
	/**
	 * 查询块存储
	 */
	@GET
	@Path("/QueryBSResp")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp QueryBSReq(@Context HttpServletRequest request){
		
		String BSID = request.getParameter("BSID");
		
		//实例化返回值对象
		BSResp bSResp = new BSResp();
		try{
			//登录
			OSClientV2 os = OSFactory.builderV2()
		            .endpoint("http://10.50.0.11:5000/v2.0")
		        .credentials("admin","ADMIN_PASS")
		        .tenantName("admin")
		        .authenticate();
			System.out.println("authentication complate");

			
			//安装volumeID来查询制定的volume
			Volume volume = os.blockStorage().volumes().get(BSID);
			
			
				
			
			bSResp.setBSID(volume.getId());
			bSResp.setBSName(volume.getName());
			bSResp.setBSSize(volume.getSize());
			
			
			
			String status_name = volume.getStatus().name();
			if(status_name.equals("AVAILABLE")){
				bSResp.setTier(1);
			}else if(status_name.equals("ATTACHING")){
				bSResp.setTier(2);
			}else if(status_name.equals("BUILDING")){
				bSResp.setTier(3);
			}else if(status_name.equals("DETTACHING")){
				bSResp.setTier(4);
			}else{
				bSResp.setTier(5);
			}
			
			

			return bSResp;
			
		}catch(Exception e){
			e.printStackTrace();
			// 异常信息
			bSResp.setFaultstring(e.getMessage());
		}
		
		return bSResp;
	}
	
	
	
	/**
	 * 更新块磁盘volume
	 */
	@POST
	@Path("/UpdateBSResp")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp UpdateBSReq(@Context HttpServletRequest request){
		String BSID = request.getParameter("BSID");
		
		String VolSize = request.getParameter("VolSize");
		
		String BSName = request.getParameter("BSName");
		
		
		
		BaseResp baseResp=new BaseResp();
		
		
		try{
			//更新快磁盘操作
			//登录
			OSClientV2 os = OSFactory.builderV2()
	                .endpoint("http://10.50.0.11:5000/v2.0")
	            .credentials("admin","ADMIN_PASS")
	            .tenantName("admin")
	            .authenticate();
			System.out.println("authentication complate");
				
			
			// list specific volume by id
			Volume volume = os.blockStorage().volumes().get(BSID);   // parameter = bSID
			
			Integer volSize = Integer.parseInt(VolSize);
			//update block storage size
			ActionResponse ar1 = os.blockStorage().volumes().extend(volume.getId(), volSize);
			
			if(!ar1.isSuccess()){
				baseResp.setFaultstring(ar1.getFault());
				return baseResp;
			}else{
				if(BSName != null){
					//update block storage name 
					ar1 = os.blockStorage().volumes().update(volume.getId(), BSName, BSName+"磁盘");
					if(!ar1.isSuccess()){
						baseResp.setFaultstring(ar1.getFault());
					}
				}
			}
			
						
			return baseResp;
			
		}catch(Exception e){
			e.printStackTrace();
			// 异常信息
			baseResp.setFaultstring(e.getMessage());
		}
		
		
		return baseResp;
	}
	
	/**
	 * 加载块存储
	 */
	@GET
	@Path("/AttachBSReq")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp AttachBSReq(@Context HttpServletRequest request){
		
		
		return null;
	}
	
	
}
